import Vue from 'vue'
import VueRouter from 'vue-router'

// 路由懒加载
const Login = () => import('../components/Login')
const Home = () => import('../components/Home')
const Welcome = () => import('../components/Welcome')
const Users = () => import('../components/user/Users')
const Rights = () => import('../components/power/Rights')
const Roles = () => import('../components/power/Roles')
const Category = () => import('../components/goods/Category')
const Goods = () => import('../components/goods/Goods')
const Params = () => import('../components/goods/Params')
const Add = () => import('../components/goods/Add')
const Reports = () => import('../components/report/Reports')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    // redirect: '/welcome',
    children: [
      { path: '', redirect: 'welcome' },
      { path: 'welcome', component: Welcome },
      { path: 'users', component: Users },
      { path: 'rights', component: Rights },
      { path: 'roles', component: Roles },
      { path: 'categories', component: Category },
      { path: 'goods', component: Goods },
      { path: 'params', component: Params },
      { path: 'add', component: Add },
      { path: 'reports', component: Reports }
    ]
  }
]
const router = new VueRouter({
  routes
})

// 挂载路由导航守卫(错误  未解决)
// router.beforeEach((to, from, next) => {
//   // to 将要访问的路径
//   // from 从哪个路径而来
//   // next函数 ，表示放行
//   //    next（） 放行   next（'/login'） 强制跳转
//   if (to.path === '/path') return next()
//   // 获取token
//   const tokenStr = window.sessionStorage.getItem('token')
//   if (!tokenStr) return next('/login')
//   next()
// })

export default router
